from aip import AipOcr
import configparser

# API 接口  爬虫

class baiDuAPI(object):
	"""百度文字识别"""
	
	#初始化
	def __init__(self):
		print('初始化')
		#实例化
		target = configparser.ConfigParser()
		#读取
		target.read('password.ini')
		#读取具体内容
		APPID = target.get('GD','AppID')
		APIKey = target.get('GD','APIKey')
		SecretKey = target.get('GD','SecretKey')
		
		self.client = AipOcr(APPID,APIKey,SecretKey)
		
	def picture2Text(self,filePath):
		'''识别文字'''
		#print('文字识别转换')
		image = baiDuAPI.getPicture(filePath)
		texts = self.client.basicGeneral(image)
		
		allTexts = ''
		for words in texts['words_result']:
			allTexts = allTexts + ''.join(words.get('words',''))
		
		print('======以下是识别内容(ctrl+V 可直接粘贴)======')
		print(allTexts)
		print('==========================')
		return allTexts
		
	@staticmethod
	def getPicture(filePath):
		'''读取图片'''
		with open(filePath,'rb') as fp:
			return fp.read()
		

if __name__ == '__main__':
	# 实例化对象
	baiduapi = baiDuAPI()
	
	baiduapi.picture2Text('imageGrab.png')
	
	
	
	
	